package com.apes.scm.card.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.product.model.Brand;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.repository.ClassifyRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @program:
 * @description:
 * @author: lws
 * @create: 2023/3/7 16:00
 **/

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsuranceCard.CONST_TABLE_NAME)
public class InsuranceCard extends Model{
    public static final String CONST_TABLE_NAME = "insurance_Card";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "保险卡券编码", padding = 12)
    private String id;

    @Comment("商品编码")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "classify_id", referencedColumnName = "id")
    @Comment("品类")
    private Classify classify;

    @Transient
    private String classifyId; // 前端展示
    public String getClassifyId(){
        if (classify != null && classifyId == null){
            return classify.getId();
        }
        return classifyId;
    }

    public void setClassifyId(String classifyId){
        this.classifyId = classifyId;
        if (this.classifyId != null){
            this.classify = SpringManager.getBean(ClassifyRepository.class).findOne(classifyId);
        }
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", referencedColumnName = "id", nullable = false)
    @Comment("品牌")
    private Brand brand;

    @States(name = "状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "delete", name = "删除")
    })
    @Comment("状态")
    private String state;

}
